import { defineStore } from 'pinia'
import { store } from '../index'
// import { getInfoApi } from '@/api/common/account'
// import type { RouteRecordRaw } from 'vue-router'

interface AccountState {
  id: string
  name: string
  avatar: string
  isPower: boolean
  rights: any[]
  module: { id: string; name: string }
  settings: Record<string, any>
  smId: string
}

export const useAccountStore = defineStore('account', {
  state: (): AccountState => {
    return {
      id: '',
      name: '',
      avatar: '',
      isPower: false,
      rights: [],
      module: { id: '218849772830720000', name: '账户信息' },
      settings: {},
      smId: '215350096886235155'
    }
  },
  getters: {
    getID(): string {
      return this.id
    },
    getName(): string {
      return this.name
    },
    getAvatar(): string {
      return this.avatar
    },
    getIsPower(): boolean {
      return this.isPower
    },
    getRights(): any[] {
      return this.rights
    },
    getModule(): any {
      return this.module
    }
  },
  actions: {
    setID(id: string) {
      this.id = id
    },
    setName(name: string) {
      this.name = name
    },
    setAvatar(avatar: string) {
      this.avatar = avatar
    },
    setIsPower(isPower: boolean) {
      this.isPower = isPower
    },
    setRights(rights: any[]) {
      this.rights = rights
    },
    setModule(module: any) {
      this.module = module
    },
    setSettings(settings: Record<string, any>) {
      this.settings = settings
    },
    // // API:获取用户信息
    // async getAccount() {
    //   try {
    //     const response = await getInfoApi({ smId: this.smId })
    //     const { MsgCode, MsgData, MsgError } = response

    //     if (MsgCode === '200') {
    //       const dtItem = typeof MsgData === 'string' ? JSON.parse(MsgData) : MsgData

    //       if (dtItem.IsAdmin) {
    //         this.id = dtItem.Id
    //         this.name = dtItem.Name
    //         this.avatar = '' // '/static/images/avatar/default.jpg'
    //         this.isPower = true
    //         this.rights = dtItem.Rights
    //         this.settings = dtItem.Settings

    //         const isAdmin = dtItem.IsAdmin ? 1 : 0
    //         await this.setProperty({ key: 'isAdmin', value: isAdmin })

    //         if (dtItem.Settings) {
    //           // for (const setting of dtItem.Settings) {
    //           //   switch (setting.SKey.toLowerCase()) {
    //           //     case 'themecolor':
    //           //       await this.changeSetting({ key: 'tool_theme', value: setting.SValue })
    //           //       break
    //           //     case 'laylight':
    //           //       await this.changeSetting({ key: 'tool_light', value: parseInt(setting.SValue) })
    //           //       break
    //           //   }
    //           // }
    //         }

    //         // await this.loadModule(this.module.id)
    //         return dtItem.Rights
    //       } else {
    //         throw new Error('禁止非法操作。')
    //       }
    //     } else {
    //       throw new Error(MsgError || '未知错误')
    //     }
    //   } catch (error) {
    //     // throw error
    //   }
    // },
    async setProperty({ key, value }: { key: string; value: any }) {
      if (key === 'isAdmin') {
        localStorage.setItem('account_isAdmin', JSON.stringify(value))
      }
    }
  },
  persist: true
})

export const useAccountStoreWithOut = () => {
  return useAccountStore(store)
}
